<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>tagiterator_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_dom_tagiterator_test.js.html">tagiterator_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.dom.TagIteratorTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.dom.TagIteratorTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.dom&#39;);
<a name="line19"></a>goog.require(&#39;goog.dom.TagIterator&#39;);
<a name="line20"></a>goog.require(&#39;goog.dom.TagWalkType&#39;);
<a name="line21"></a>goog.require(&#39;goog.iter&#39;);
<a name="line22"></a>goog.require(&#39;goog.iter.StopIteration&#39;);
<a name="line23"></a>goog.require(&#39;goog.testing.dom&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line25"></a>
<a name="line26"></a>var it;
<a name="line27"></a>var pos;
<a name="line28"></a>
<a name="line29"></a>function assertStartTag(type) {
<a name="line30"></a>  assertEquals(&#39;Position &#39; + pos + &#39; should be start tag&#39;,
<a name="line31"></a>      goog.dom.TagWalkType.START_TAG, it.tagType);
<a name="line32"></a>  assertTrue(&#39;isStartTag should return true&#39;, it.isStartTag());
<a name="line33"></a>  assertFalse(&#39;isEndTag should return false&#39;, it.isEndTag());
<a name="line34"></a>  assertFalse(&#39;isNonElement should return false&#39;, it.isNonElement());
<a name="line35"></a>  assertEquals(&#39;Position &#39; + pos + &#39; should be &#39; + type, type,
<a name="line36"></a>      it.node.tagName);
<a name="line37"></a>}
<a name="line38"></a>
<a name="line39"></a>function assertEndTag(type) {
<a name="line40"></a>  assertEquals(&#39;Position &#39; + pos + &#39; should be end tag&#39;,
<a name="line41"></a>      goog.dom.TagWalkType.END_TAG, it.tagType);
<a name="line42"></a>  assertFalse(&#39;isStartTag should return false&#39;, it.isStartTag());
<a name="line43"></a>  assertTrue(&#39;isEndTag should return true&#39;, it.isEndTag());
<a name="line44"></a>  assertFalse(&#39;isNonElement should return false&#39;, it.isNonElement());
<a name="line45"></a>  assertEquals(&#39;Position &#39; + pos + &#39; should be &#39; + type, type,
<a name="line46"></a>      it.node.tagName);
<a name="line47"></a>}
<a name="line48"></a>
<a name="line49"></a>function assertTextNode(value) {
<a name="line50"></a>  assertEquals(&#39;Position &#39; + pos + &#39; should be text node&#39;,
<a name="line51"></a>      goog.dom.TagWalkType.OTHER, it.tagType);
<a name="line52"></a>  assertFalse(&#39;isStartTag should return false&#39;, it.isStartTag());
<a name="line53"></a>  assertFalse(&#39;isEndTag should return false&#39;, it.isEndTag());
<a name="line54"></a>  assertTrue(&#39;isNonElement should return true&#39;, it.isNonElement());
<a name="line55"></a>  assertEquals(&#39;Position &#39; + pos + &#39; should be &quot;&#39; + value + &#39;&quot;&#39;, value,
<a name="line56"></a>      it.node.nodeValue);
<a name="line57"></a>}
<a name="line58"></a>
<a name="line59"></a>function testBasicHTML() {
<a name="line60"></a>  it = new goog.dom.TagIterator(goog.dom.getElement(&#39;test&#39;));
<a name="line61"></a>  pos = 0;
<a name="line62"></a>
<a name="line63"></a>  goog.iter.forEach(it, function() {
<a name="line64"></a>    pos++;
<a name="line65"></a>    switch (pos) {
<a name="line66"></a>      case 1:
<a name="line67"></a>        assertStartTag(&#39;DIV&#39;);
<a name="line68"></a>        break;
<a name="line69"></a>      case 2:
<a name="line70"></a>        assertStartTag(&#39;A&#39;);
<a name="line71"></a>        break;
<a name="line72"></a>      case 3:
<a name="line73"></a>        assertTextNode(&#39;T&#39;);
<a name="line74"></a>        break;
<a name="line75"></a>      case 4:
<a name="line76"></a>        assertStartTag(&#39;B&#39;);
<a name="line77"></a>        assertEquals(&#39;Depth at &lt;B&gt; should be 3&#39;, 3, it.depth);
<a name="line78"></a>        break;
<a name="line79"></a>      case 5:
<a name="line80"></a>        assertTextNode(&#39;e&#39;);
<a name="line81"></a>        break;
<a name="line82"></a>      case 6:
<a name="line83"></a>        assertEndTag(&#39;B&#39;);
<a name="line84"></a>        break;
<a name="line85"></a>      case 7:
<a name="line86"></a>        assertTextNode(&#39;xt&#39;);
<a name="line87"></a>        break;
<a name="line88"></a>      case 8:
<a name="line89"></a>        assertEndTag(&#39;A&#39;);
<a name="line90"></a>        break;
<a name="line91"></a>      case 9:
<a name="line92"></a>        assertStartTag(&#39;SPAN&#39;);
<a name="line93"></a>        break;
<a name="line94"></a>      case 10:
<a name="line95"></a>        assertEndTag(&#39;SPAN&#39;);
<a name="line96"></a>        break;
<a name="line97"></a>      case 11:
<a name="line98"></a>        assertStartTag(&#39;P&#39;);
<a name="line99"></a>        break;
<a name="line100"></a>      case 12:
<a name="line101"></a>        assertTextNode(&#39;Text&#39;);
<a name="line102"></a>        break;
<a name="line103"></a>      case 13:
<a name="line104"></a>        assertEndTag(&#39;P&#39;);
<a name="line105"></a>        break;
<a name="line106"></a>      case 14:
<a name="line107"></a>        assertEndTag(&#39;DIV&#39;);
<a name="line108"></a>        assertEquals(&#39;Depth at end should be 0&#39;, 0, it.depth);
<a name="line109"></a>        break;
<a name="line110"></a>      default:
<a name="line111"></a>        throw goog.iter.StopIteration;
<a name="line112"></a>    }
<a name="line113"></a>  });
<a name="line114"></a>}
<a name="line115"></a>
<a name="line116"></a>function testSkipTag() {
<a name="line117"></a>  it = new goog.dom.TagIterator(goog.dom.getElement(&#39;test&#39;));
<a name="line118"></a>  pos = 0;
<a name="line119"></a>
<a name="line120"></a>  goog.iter.forEach(it, function() {
<a name="line121"></a>    pos++;
<a name="line122"></a>    switch (pos) {
<a name="line123"></a>      case 1:
<a name="line124"></a>        assertStartTag(&#39;DIV&#39;);
<a name="line125"></a>        break;
<a name="line126"></a>      case 2:
<a name="line127"></a>        assertStartTag(&#39;A&#39;);
<a name="line128"></a>        it.skipTag();
<a name="line129"></a>        break;
<a name="line130"></a>      case 3:
<a name="line131"></a>        assertStartTag(&#39;SPAN&#39;);
<a name="line132"></a>        break;
<a name="line133"></a>      case 4:
<a name="line134"></a>        assertEndTag(&#39;SPAN&#39;);
<a name="line135"></a>        break;
<a name="line136"></a>      case 5:
<a name="line137"></a>        assertStartTag(&#39;P&#39;);
<a name="line138"></a>        break;
<a name="line139"></a>      case 6:
<a name="line140"></a>        assertTextNode(&#39;Text&#39;);
<a name="line141"></a>        break;
<a name="line142"></a>      case 7:
<a name="line143"></a>        assertEndTag(&#39;P&#39;);
<a name="line144"></a>        break;
<a name="line145"></a>      case 8:
<a name="line146"></a>        assertEndTag(&#39;DIV&#39;);
<a name="line147"></a>        assertEquals(&#39;Depth at end should be 0&#39;, 0, it.depth);
<a name="line148"></a>        break;
<a name="line149"></a>      default:
<a name="line150"></a>        throw goog.iter.StopIteration;
<a name="line151"></a>    }
<a name="line152"></a>  });
<a name="line153"></a>}
<a name="line154"></a>
<a name="line155"></a>function testRestartTag() {
<a name="line156"></a>  it = new goog.dom.TagIterator(goog.dom.getElement(&#39;test&#39;));
<a name="line157"></a>  pos = 0;
<a name="line158"></a>  var done = false;
<a name="line159"></a>
<a name="line160"></a>  goog.iter.forEach(it, function() {
<a name="line161"></a>    pos++;
<a name="line162"></a>    switch (pos) {
<a name="line163"></a>      case 1:
<a name="line164"></a>        assertStartTag(&#39;DIV&#39;);
<a name="line165"></a>        break;
<a name="line166"></a>      case 2:
<a name="line167"></a>        assertStartTag(&#39;A&#39;);
<a name="line168"></a>        it.skipTag();
<a name="line169"></a>        break;
<a name="line170"></a>      case 3:
<a name="line171"></a>        assertStartTag(&#39;SPAN&#39;);
<a name="line172"></a>        break;
<a name="line173"></a>      case 4:
<a name="line174"></a>        assertEndTag(&#39;SPAN&#39;);
<a name="line175"></a>        break;
<a name="line176"></a>      case 5:
<a name="line177"></a>        assertStartTag(&#39;P&#39;);
<a name="line178"></a>        break;
<a name="line179"></a>      case 6:
<a name="line180"></a>        assertTextNode(&#39;Text&#39;);
<a name="line181"></a>        break;
<a name="line182"></a>      case 7:
<a name="line183"></a>        assertEndTag(&#39;P&#39;);
<a name="line184"></a>        break;
<a name="line185"></a>      case 8:
<a name="line186"></a>        assertEndTag(&#39;DIV&#39;);
<a name="line187"></a>        assertEquals(&#39;Depth at end should be 0&#39;, 0, it.depth);
<a name="line188"></a>
<a name="line189"></a>        // Do them all again, starting after this element.
<a name="line190"></a>        if (!done) {
<a name="line191"></a>          pos = 1;
<a name="line192"></a>          it.restartTag();
<a name="line193"></a>          done = true;
<a name="line194"></a>        }
<a name="line195"></a>        break;
<a name="line196"></a>      default:
<a name="line197"></a>        throw goog.iter.StopIteration;
<a name="line198"></a>    }
<a name="line199"></a>  });
<a name="line200"></a>}
<a name="line201"></a>
<a name="line202"></a>
<a name="line203"></a>function testSkipTagReverse() {
<a name="line204"></a>  it = new goog.dom.TagIterator(goog.dom.getElement(&#39;test&#39;), true);
<a name="line205"></a>  pos = 9;
<a name="line206"></a>
<a name="line207"></a>  goog.iter.forEach(it, function() {
<a name="line208"></a>    pos--;
<a name="line209"></a>    switch (pos) {
<a name="line210"></a>      case 1:
<a name="line211"></a>        assertStartTag(&#39;DIV&#39;);
<a name="line212"></a>        assertEquals(&#39;Depth at end should be 0&#39;, 0, it.depth);
<a name="line213"></a>        break;
<a name="line214"></a>      case 2:
<a name="line215"></a>        assertEndTag(&#39;A&#39;);
<a name="line216"></a>        it.skipTag();
<a name="line217"></a>        break;
<a name="line218"></a>      case 3:
<a name="line219"></a>        assertStartTag(&#39;SPAN&#39;);
<a name="line220"></a>        break;
<a name="line221"></a>      case 4:
<a name="line222"></a>        assertEndTag(&#39;SPAN&#39;);
<a name="line223"></a>        break;
<a name="line224"></a>      case 5:
<a name="line225"></a>        assertStartTag(&#39;P&#39;);
<a name="line226"></a>        break;
<a name="line227"></a>      case 6:
<a name="line228"></a>        assertTextNode(&#39;Text&#39;);
<a name="line229"></a>        break;
<a name="line230"></a>      case 7:
<a name="line231"></a>        assertEndTag(&#39;P&#39;);
<a name="line232"></a>        break;
<a name="line233"></a>      case 8:
<a name="line234"></a>        assertEndTag(&#39;DIV&#39;);
<a name="line235"></a>        break;
<a name="line236"></a>      default:
<a name="line237"></a>        throw goog.iter.StopIteration;
<a name="line238"></a>    }
<a name="line239"></a>  });
<a name="line240"></a>}
<a name="line241"></a>
<a name="line242"></a>
<a name="line243"></a>function testUnclosedLI() {
<a name="line244"></a>  it = new goog.dom.TagIterator(goog.dom.getElement(&#39;test2&#39;));
<a name="line245"></a>  pos = 0;
<a name="line246"></a>
<a name="line247"></a>  goog.iter.forEach(it, function() {
<a name="line248"></a>    pos++;
<a name="line249"></a>    switch (pos) {
<a name="line250"></a>      case 1:
<a name="line251"></a>        assertStartTag(&#39;UL&#39;);
<a name="line252"></a>        break;
<a name="line253"></a>      case 2:
<a name="line254"></a>        assertStartTag(&#39;LI&#39;);
<a name="line255"></a>        assertEquals(&#39;Depth at &lt;LI&gt; should be 2&#39;, 2, it.depth);
<a name="line256"></a>        break;
<a name="line257"></a>      case 3:
<a name="line258"></a>        assertTextNode(&#39;Not&#39;);
<a name="line259"></a>        break;
<a name="line260"></a>      case 4:
<a name="line261"></a>        assertEndTag(&#39;LI&#39;);
<a name="line262"></a>        break;
<a name="line263"></a>      case 5:
<a name="line264"></a>        assertStartTag(&#39;LI&#39;);
<a name="line265"></a>        assertEquals(&#39;Depth at second &lt;LI&gt; should be 2&#39;, 2, it.depth);
<a name="line266"></a>        break;
<a name="line267"></a>      case 6:
<a name="line268"></a>        assertTextNode(&#39;Closed&#39;);
<a name="line269"></a>        break;
<a name="line270"></a>      case 7:
<a name="line271"></a>        assertEndTag(&#39;LI&#39;);
<a name="line272"></a>        break;
<a name="line273"></a>      case 8:
<a name="line274"></a>        assertEndTag(&#39;UL&#39;);
<a name="line275"></a>        assertEquals(&#39;Depth at end should be 0&#39;, 0, it.depth);
<a name="line276"></a>        break;
<a name="line277"></a>      default:
<a name="line278"></a>        throw goog.iter.StopIteration;
<a name="line279"></a>    }
<a name="line280"></a>  });
<a name="line281"></a>}
<a name="line282"></a>
<a name="line283"></a>function testReversedUnclosedLI() {
<a name="line284"></a>  it = new goog.dom.TagIterator(goog.dom.getElement(&#39;test2&#39;), true);
<a name="line285"></a>  pos = 9;
<a name="line286"></a>
<a name="line287"></a>  goog.iter.forEach(it, function() {
<a name="line288"></a>    pos--;
<a name="line289"></a>    switch (pos) {
<a name="line290"></a>      case 1:
<a name="line291"></a>        assertStartTag(&#39;UL&#39;);
<a name="line292"></a>        assertEquals(&#39;Depth at start should be 0&#39;, 0, it.depth);
<a name="line293"></a>        break;
<a name="line294"></a>      case 2:
<a name="line295"></a>        assertStartTag(&#39;LI&#39;);
<a name="line296"></a>        break;
<a name="line297"></a>      case 3:
<a name="line298"></a>        assertTextNode(&#39;Not&#39;);
<a name="line299"></a>        break;
<a name="line300"></a>      case 4:
<a name="line301"></a>        assertEndTag(&#39;LI&#39;);
<a name="line302"></a>        assertEquals(&#39;Depth at &lt;LI&gt; should be 2&#39;, 2, it.depth);
<a name="line303"></a>        break;
<a name="line304"></a>      case 5:
<a name="line305"></a>        assertStartTag(&#39;LI&#39;);
<a name="line306"></a>        break;
<a name="line307"></a>      case 6:
<a name="line308"></a>        assertTextNode(&#39;Closed&#39;);
<a name="line309"></a>        break;
<a name="line310"></a>      case 7:
<a name="line311"></a>        assertEndTag(&#39;LI&#39;);
<a name="line312"></a>        assertEquals(&#39;Depth at second &lt;LI&gt; should be 2&#39;, 2, it.depth);
<a name="line313"></a>        break;
<a name="line314"></a>      case 8:
<a name="line315"></a>        assertEndTag(&#39;UL&#39;);
<a name="line316"></a>        break;
<a name="line317"></a>      default:
<a name="line318"></a>        throw goog.iter.StopIteration;
<a name="line319"></a>    }
<a name="line320"></a>  });
<a name="line321"></a>}
<a name="line322"></a>
<a name="line323"></a>function testConstrained() {
<a name="line324"></a>  it = new goog.dom.TagIterator(goog.dom.getElement(&#39;test3&#39;), false, false);
<a name="line325"></a>  pos = 0;
<a name="line326"></a>
<a name="line327"></a>  goog.iter.forEach(it, function() {
<a name="line328"></a>    pos++;
<a name="line329"></a>    switch (pos) {
<a name="line330"></a>      case 1:
<a name="line331"></a>        assertStartTag(&#39;DIV&#39;);
<a name="line332"></a>        break;
<a name="line333"></a>      case 2:
<a name="line334"></a>        assertTextNode(&#39;text&#39;);
<a name="line335"></a>        break;
<a name="line336"></a>      case 3:
<a name="line337"></a>        assertEndTag(&#39;DIV&#39;);
<a name="line338"></a>        break;
<a name="line339"></a>    }
<a name="line340"></a>  });
<a name="line341"></a>
<a name="line342"></a>  assertEquals(&#39;Constrained iterator should stop at position 3.&#39;, 3, pos);
<a name="line343"></a>}
<a name="line344"></a>
<a name="line345"></a>function testUnconstrained() {
<a name="line346"></a>  it = new goog.dom.TagIterator(goog.dom.getElement(&#39;test3&#39;), false, true);
<a name="line347"></a>  pos = 0;
<a name="line348"></a>
<a name="line349"></a>  goog.iter.forEach(it, function() {
<a name="line350"></a>    pos++;
<a name="line351"></a>    switch (pos) {
<a name="line352"></a>      case 1:
<a name="line353"></a>        assertStartTag(&#39;DIV&#39;);
<a name="line354"></a>        break;
<a name="line355"></a>      case 2:
<a name="line356"></a>        assertTextNode(&#39;text&#39;);
<a name="line357"></a>        break;
<a name="line358"></a>      case 3:
<a name="line359"></a>        assertEndTag(&#39;DIV&#39;);
<a name="line360"></a>        break;
<a name="line361"></a>    }
<a name="line362"></a>  });
<a name="line363"></a>
<a name="line364"></a>  assertNotEquals(&#39;Unonstrained iterator should not stop at position 3.&#39;, 3,
<a name="line365"></a>      pos);
<a name="line366"></a>}
<a name="line367"></a>
<a name="line368"></a>function testConstrainedText() {
<a name="line369"></a>  it = new goog.dom.TagIterator(goog.dom.getElement(&#39;test3&#39;).firstChild,
<a name="line370"></a>      false, false);
<a name="line371"></a>  pos = 0;
<a name="line372"></a>
<a name="line373"></a>  goog.iter.forEach(it, function() {
<a name="line374"></a>    pos++;
<a name="line375"></a>    switch (pos) {
<a name="line376"></a>      case 1:
<a name="line377"></a>        assertTextNode(&#39;text&#39;);
<a name="line378"></a>        break;
<a name="line379"></a>    }
<a name="line380"></a>  });
<a name="line381"></a>
<a name="line382"></a>  assertEquals(&#39;Constrained text iterator should stop at position 1.&#39;, 1,
<a name="line383"></a>      pos);
<a name="line384"></a>}
<a name="line385"></a>
<a name="line386"></a>function testReverseConstrained() {
<a name="line387"></a>  it = new goog.dom.TagIterator(goog.dom.getElement(&#39;test3&#39;), true, false);
<a name="line388"></a>  pos = 4;
<a name="line389"></a>
<a name="line390"></a>  goog.iter.forEach(it, function() {
<a name="line391"></a>    pos--;
<a name="line392"></a>    switch (pos) {
<a name="line393"></a>      case 1:
<a name="line394"></a>        assertStartTag(&#39;DIV&#39;);
<a name="line395"></a>        break;
<a name="line396"></a>      case 2:
<a name="line397"></a>        assertTextNode(&#39;text&#39;);
<a name="line398"></a>        break;
<a name="line399"></a>      case 3:
<a name="line400"></a>        assertEndTag(&#39;DIV&#39;);
<a name="line401"></a>        break;
<a name="line402"></a>    }
<a name="line403"></a>  });
<a name="line404"></a>
<a name="line405"></a>  assertEquals(&#39;Constrained reversed iterator should stop at position 1.&#39;, 1,
<a name="line406"></a>      pos);
<a name="line407"></a>}
<a name="line408"></a>
<a name="line409"></a>function testSpliceRemoveSingleNode() {
<a name="line410"></a>  var testDiv = goog.dom.getElement(&#39;testSplice&#39;);
<a name="line411"></a>  testDiv.innerHTML = &#39;&lt;br/&gt;&#39;;
<a name="line412"></a>  it = new goog.dom.TagIterator(testDiv.firstChild);
<a name="line413"></a>
<a name="line414"></a>  goog.iter.forEach(it, function(node, dummy, i) {
<a name="line415"></a>    i.splice();
<a name="line416"></a>  });
<a name="line417"></a>
<a name="line418"></a>  assertEquals(&#39;Node not removed&#39;, 0, testDiv.childNodes.length);
<a name="line419"></a>}
<a name="line420"></a>
<a name="line421"></a>function testSpliceRemoveFirstTextNode() {
<a name="line422"></a>  var testDiv = goog.dom.getElement(&#39;testSplice&#39;);
<a name="line423"></a>  testDiv.innerHTML = &#39;hello&lt;b&gt;world&lt;/b&gt;&lt;em&gt;goodbye&lt;/em&gt;&#39;;
<a name="line424"></a>  it = new goog.dom.TagIterator(testDiv.firstChild, false, true);
<a name="line425"></a>
<a name="line426"></a>  goog.iter.forEach(it, function(node, dummy, i) {
<a name="line427"></a>    if (node.nodeType == 3 &amp;&amp; node.data == &#39;hello&#39;) {
<a name="line428"></a>      i.splice();
<a name="line429"></a>    }
<a name="line430"></a>    if (node.nodeName == &#39;EM&#39;) {
<a name="line431"></a>      i.splice(goog.dom.createDom(&#39;I&#39;, null, node.childNodes));
<a name="line432"></a>    }
<a name="line433"></a>  });
<a name="line434"></a>
<a name="line435"></a>  goog.testing.dom.assertHtmlMatches(&#39;&lt;b&gt;world&lt;/b&gt;&lt;i&gt;goodbye&lt;/i&gt;&#39;,
<a name="line436"></a>      testDiv.innerHTML);
<a name="line437"></a>}
<a name="line438"></a>
<a name="line439"></a>function testSpliceReplaceFirstTextNode() {
<a name="line440"></a>  var testDiv = goog.dom.getElement(&#39;testSplice&#39;);
<a name="line441"></a>  testDiv.innerHTML = &#39;hello&lt;b&gt;world&lt;/b&gt;&#39;;
<a name="line442"></a>  it = new goog.dom.TagIterator(testDiv.firstChild, false, true);
<a name="line443"></a>
<a name="line444"></a>  goog.iter.forEach(it, function(node, dummy, i) {
<a name="line445"></a>    if (node.nodeType == 3 &amp;&amp; node.data == &#39;hello&#39;) {
<a name="line446"></a>      i.splice(goog.dom.createDom(&#39;EM&#39;, null, &#39;HELLO&#39;));
<a name="line447"></a>    } else if (node.nodeName == &#39;EM&#39;) {
<a name="line448"></a>      i.splice(goog.dom.createDom(&#39;I&#39;, null, node.childNodes));
<a name="line449"></a>    }
<a name="line450"></a>  });
<a name="line451"></a>
<a name="line452"></a>  goog.testing.dom.assertHtmlMatches(&#39;&lt;i&gt;HELLO&lt;/i&gt;&lt;b&gt;world&lt;/b&gt;&#39;,
<a name="line453"></a>      testDiv.innerHTML);
<a name="line454"></a>}
<a name="line455"></a>
<a name="line456"></a>function testSpliceReplaceSingleNode() {
<a name="line457"></a>  var testDiv = goog.dom.getElement(&#39;testSplice&#39;);
<a name="line458"></a>  testDiv.innerHTML = &#39;&lt;br/&gt;&#39;;
<a name="line459"></a>  it = new goog.dom.TagIterator(testDiv.firstChild);
<a name="line460"></a>
<a name="line461"></a>  goog.iter.forEach(it, function(node, dummy, i) {
<a name="line462"></a>    i.splice(goog.dom.createDom(&#39;link&#39;), goog.dom.createDom(&#39;img&#39;));
<a name="line463"></a>  });
<a name="line464"></a>
<a name="line465"></a>  goog.testing.dom.assertHtmlMatches(&#39;&lt;link&gt;&lt;img&gt;&#39;, testDiv.innerHTML);
<a name="line466"></a>}
<a name="line467"></a>
<a name="line468"></a>function testSpliceFlattenSingleNode() {
<a name="line469"></a>  var testDiv = goog.dom.getElement(&#39;testSplice&#39;);
<a name="line470"></a>  testDiv.innerHTML = &#39;&lt;div&gt;&lt;b&gt;one&lt;/b&gt;two&lt;i&gt;three&lt;/i&gt;&lt;/div&gt;&#39;;
<a name="line471"></a>  it = new goog.dom.TagIterator(testDiv.firstChild);
<a name="line472"></a>
<a name="line473"></a>  goog.iter.forEach(it, function(node, dummy, i) {
<a name="line474"></a>    i.splice(node.childNodes);
<a name="line475"></a>  });
<a name="line476"></a>
<a name="line477"></a>  goog.testing.dom.assertHtmlMatches(&#39;&lt;b&gt;one&lt;/b&gt;two&lt;i&gt;three&lt;/i&gt;&#39;,
<a name="line478"></a>      testDiv.innerHTML);
<a name="line479"></a>}
<a name="line480"></a>
<a name="line481"></a>function testSpliceMiddleNode() {
<a name="line482"></a>  var testDiv = goog.dom.getElement(&#39;testSplice&#39;);
<a name="line483"></a>  testDiv.innerHTML = &#39;a&lt;b&gt;hello&lt;span&gt;world&lt;/span&gt;&lt;/b&gt;c&#39;;
<a name="line484"></a>  it = new goog.dom.TagIterator(testDiv);
<a name="line485"></a>
<a name="line486"></a>  goog.iter.forEach(it, function(node, dummy, i) {
<a name="line487"></a>    if (node.nodeName == &#39;B&#39;) {
<a name="line488"></a>      i.splice(goog.dom.createDom(&#39;IMG&#39;));
<a name="line489"></a>    }
<a name="line490"></a>  });
<a name="line491"></a>
<a name="line492"></a>  goog.testing.dom.assertHtmlMatches(&#39;a&lt;img&gt;c&#39;, testDiv.innerHTML);
<a name="line493"></a>}
<a name="line494"></a>
<a name="line495"></a>function testSpliceMiddleNodeReversed() {
<a name="line496"></a>  var testDiv = goog.dom.getElement(&#39;testSplice&#39;);
<a name="line497"></a>  testDiv.innerHTML = &#39;a&lt;b&gt;hello&lt;span&gt;world&lt;/span&gt;&lt;/b&gt;c&#39;;
<a name="line498"></a>  it = new goog.dom.TagIterator(testDiv, true);
<a name="line499"></a>
<a name="line500"></a>  goog.iter.forEach(it, function(node, dummy, i) {
<a name="line501"></a>    if (node.nodeName == &#39;B&#39;) {
<a name="line502"></a>      i.splice(goog.dom.createDom(&#39;IMG&#39;));
<a name="line503"></a>    }
<a name="line504"></a>  });
<a name="line505"></a>
<a name="line506"></a>  goog.testing.dom.assertHtmlMatches(&#39;a&lt;img&gt;c&#39;, testDiv.innerHTML);
<a name="line507"></a>}
<a name="line508"></a>
<a name="line509"></a>function testSpliceMiddleNodeAtEndTag() {
<a name="line510"></a>  var testDiv = goog.dom.getElement(&#39;testSplice&#39;);
<a name="line511"></a>  testDiv.innerHTML = &#39;a&lt;b&gt;hello&lt;span&gt;world&lt;/span&gt;&lt;/b&gt;c&#39;;
<a name="line512"></a>  it = new goog.dom.TagIterator(testDiv);
<a name="line513"></a>
<a name="line514"></a>  goog.iter.forEach(it, function(node, dummy, i) {
<a name="line515"></a>    if (node.tagName == &#39;B&#39; &amp;&amp; i.isEndTag()) {
<a name="line516"></a>      i.splice(goog.dom.createDom(&#39;IMG&#39;));
<a name="line517"></a>    }
<a name="line518"></a>  });
<a name="line519"></a>
<a name="line520"></a>  goog.testing.dom.assertHtmlMatches(&#39;a&lt;img&gt;c&#39;, testDiv.innerHTML);
<a name="line521"></a>}
<a name="line522"></a>
<a name="line523"></a>function testSpliceMultipleNodes() {
<a name="line524"></a>  var testDiv = goog.dom.getElement(&#39;testSplice&#39;);
<a name="line525"></a>  testDiv.innerHTML = &#39;&lt;strong&gt;this&lt;/strong&gt; is &lt;em&gt;from IE&lt;/em&gt;&#39;;
<a name="line526"></a>  it = new goog.dom.TagIterator(testDiv);
<a name="line527"></a>
<a name="line528"></a>  goog.iter.forEach(it, function(node, dummy, i) {
<a name="line529"></a>    var replace = null;
<a name="line530"></a>    if (node.nodeName == &#39;STRONG&#39;) {
<a name="line531"></a>      replace = goog.dom.createDom(&#39;B&#39;, null, node.childNodes);
<a name="line532"></a>    } else if (node.nodeName == &#39;EM&#39;) {
<a name="line533"></a>      replace = goog.dom.createDom(&#39;I&#39;, null, node.childNodes);
<a name="line534"></a>    }
<a name="line535"></a>    if (replace) {
<a name="line536"></a>      i.splice(replace);
<a name="line537"></a>    }
<a name="line538"></a>  });
<a name="line539"></a>
<a name="line540"></a>  goog.testing.dom.assertHtmlMatches(&#39;&lt;b&gt;this&lt;/b&gt; is &lt;i&gt;from IE&lt;/i&gt;&#39;,
<a name="line541"></a>      testDiv.innerHTML);
<a name="line542"></a>}
<a name="line543"></a>
<a name="line544"></a>function testSpliceMultipleNodesAtEnd() {
<a name="line545"></a>  var testDiv = goog.dom.getElement(&#39;testSplice&#39;);
<a name="line546"></a>  testDiv.innerHTML = &#39;&lt;strong&gt;this&lt;/strong&gt; is &lt;em&gt;from IE&lt;/em&gt;&#39;;
<a name="line547"></a>  it = new goog.dom.TagIterator(testDiv);
<a name="line548"></a>
<a name="line549"></a>  goog.iter.forEach(it, function(node, dummy, i) {
<a name="line550"></a>    var replace = null;
<a name="line551"></a>    if (node.nodeName == &#39;STRONG&#39; &amp;&amp; i.isEndTag()) {
<a name="line552"></a>      replace = goog.dom.createDom(&#39;B&#39;, null, node.childNodes);
<a name="line553"></a>    } else if (node.nodeName == &#39;EM&#39; &amp;&amp; i.isEndTag()) {
<a name="line554"></a>      replace = goog.dom.createDom(&#39;I&#39;, null, node.childNodes);
<a name="line555"></a>    }
<a name="line556"></a>    if (replace) {
<a name="line557"></a>      i.splice(replace);
<a name="line558"></a>    }
<a name="line559"></a>  });
<a name="line560"></a>
<a name="line561"></a>  goog.testing.dom.assertHtmlMatches(&#39;&lt;b&gt;this&lt;/b&gt; is &lt;i&gt;from IE&lt;/i&gt;&#39;,
<a name="line562"></a>      testDiv.innerHTML);
<a name="line563"></a>}
<a name="line564"></a>
<a name="line565"></a>function testSpliceMultipleNodesReversed() {
<a name="line566"></a>  var testDiv = goog.dom.getElement(&#39;testSplice&#39;);
<a name="line567"></a>  testDiv.innerHTML = &#39;&lt;strong&gt;this&lt;/strong&gt; is &lt;em&gt;from IE&lt;/em&gt;&#39;;
<a name="line568"></a>  it = new goog.dom.TagIterator(testDiv, true);
<a name="line569"></a>
<a name="line570"></a>  goog.iter.forEach(it, function(node, dummy, i) {
<a name="line571"></a>    var replace = null;
<a name="line572"></a>    if (node.nodeName == &#39;STRONG&#39;) {
<a name="line573"></a>      replace = goog.dom.createDom(&#39;B&#39;, null, node.childNodes);
<a name="line574"></a>    } else if (node.nodeName == &#39;EM&#39;) {
<a name="line575"></a>      replace = goog.dom.createDom(&#39;I&#39;, null, node.childNodes);
<a name="line576"></a>    }
<a name="line577"></a>    if (replace) {
<a name="line578"></a>      i.splice(replace);
<a name="line579"></a>    }
<a name="line580"></a>  });
<a name="line581"></a>
<a name="line582"></a>  goog.testing.dom.assertHtmlMatches(&#39;&lt;b&gt;this&lt;/b&gt; is &lt;i&gt;from IE&lt;/i&gt;&#39;,
<a name="line583"></a>      testDiv.innerHTML);
<a name="line584"></a>}
</pre>


</body>
</html>
